let house_id = 0
let house_index = 0
Page({
  data: {
    dialogVisible: false,
  },
  async onShow() {
    try {
      await this.getHouseList()
    } catch (error) {
      wx.utils.toast(error.message)
    }

  },


  // 房屋列表
  async getHouseList() {
    const {
      code,
      data: houseList,
      message
    } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast(message)
    this.setData({
      houseList,
      isEmpty: houseList.length === 0
    })
  },

  swipeClose(ev) {
    const {
      position,
      instance
    } = ev.detail
    if (position === 'right') {


      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      house_id = ev.mark.id
      house_index = ev.mark.index
      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  async dialogClose(e) {
    // console.log(e);
    if (e.detail === 'confirm') {
      const res = await wx.http.delete(`/room/${house_id}`)
      this.data.houseList.splice(house_index, 1)
      // 刷新
      this.setData({
        houseList: this.data.houseList,
        isEmpty: this.data.houseList.length === 0
      })
      console.log(res);
    }
  },
  goDetail(e) {
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${e.mark.id}`,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})